#!/usr/bin/env bash
# Copyright (c) 2024 maminjie <canpool@163.com>
# SPDX-License-Identifier: MulanPSL-2.0


method_def pub

usage_pub() {
    module_usage "pub" "A product publish tool"
}

do_pub() {
    module_do "pub" "$@"
}

pub_usage_help() {
    module_usage_help pub
}

# pub_do_help subcmd
pub_do_help() {
    module_do_help pub "$1"
}

# __pub_get_subcmd cmd
#   Get the fullname of cmd
# Returns:
#   "" or fullname
__pub_get_subcmd() {
    local cmd=""
    case "${1}" in
        "h"|"help")
            cmd="help"
            ;;
        "repo"|"repository")
            cmd="repository"
            ;;
        *)
            ;;
    esac
    echo "$cmd"
}


pub_usage_repository() {
printf "repository (repo): Publish an openEuler-style yum repository

usage:
    ${PROG} pub repo DIR

params:
    DIR - directory containing the rpm package, directory hierarchy as follows:
              DIR
              ├── aarch64
              │   └── Packages
              │       ├── xxx.aarch64.rpm
              ├── source
              │   └── Packages
              │       ├── yyy.src.rpm
              └── x86_64
                  └── Packages
                      ├── zzz.x86_64.rpm
                      ...
After publishing, the repodata directory is generated under the Packages sibling directory
\n"
}

pub_do_repository() {
    if [ $# -lt 1 ]; then
        pub_usage_repository; exit
    fi
    local pkg_dirs=$(find $1 -name Packages)
    for d in $pkg_dirs; do
        local repo_dir=${d%/*}
        createrepo $repo_dir
    done
}


pub_docheck() {
    check_command "createrepo"
}
